
/**
 *
 * @author S3157015
 */

//chess class 
class ChessMan {

    private boolean chessMan;

    public ChessMan(boolean man) {
        this.chessMan = man;
    }

    public boolean toBool() {
        return chessMan;
    }
}


//chess board
class ChessBoard {

    private ChessMan[][] board;
    private ChessMan[][] oldBoard;

    ChessBoard() {
        board = new ChessMan[1 + 8][1 + 8];
        oldBoard = new ChessMan[1 + 8][1 + 8];
    }

    //init chess board
    public void initChessBoard() {
        for (int x = 1; x <= 8; x++) {
            for (int y = 1; y <= 8; y++) {
                board[x][y] = null;
                oldBoard[x][y] = null;
            }
        }

        board[4][4] = new ChessMan(AllDef.BLACK);
        board[5][5] = new ChessMan(AllDef.BLACK);
        board[4][5] = new ChessMan(AllDef.WHITE);
        board[5][4] = new ChessMan(AllDef.WHITE);
    }

    public ChessMan getChessMan(int x, int y) {
        return board[x][y];
    }

    //add new pace to board
    public void changeBoard(int x, int y, boolean who) {
        board[x][y] = null;
        board[x][y] = new ChessMan(who);
    }

    public ChessMan getOldChessMan(int x, int y) {
        return oldBoard[x][y];
    }

    public void setOldChessMan(int x, int y, boolean who) {
        oldBoard[x][y] = new ChessMan(who);
    }

    //delete all the piece from this board array
    public void initOldChessBoard() {
        for (int x = 1; x <= 8; x++) {
            for (int y = 1; y <= 8; y++) {
                oldBoard[x][y] = null;
            }
        }
    }
}

public class Players {

    private String name;
    private boolean my_turn;
    private static ChessBoard cb;
    private int score;

    public Players(String name, boolean turn) {
        this.name = name;
        this.my_turn = turn;
        cb = new ChessBoard();
        cb.initChessBoard();
        score = 0;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    // eat the piece. white to blace, black to white
    private boolean eat(int x, int y, int xChange, int yChange) {
        if (x < 1 || x > 8 || y < 1 || y > 8) {
            return AllDef.ERROR;
        }
        if (cb.getChessMan(x, y) != null) {
            return AllDef.ERROR;
        }
        int xNext = x + xChange;
        int yNext = y + yChange;

        int currX = x;
        int currY = y;

        while (true) {
            if (xNext < 1 || xNext > 8 || yNext < 1 || yNext > 8
                    || cb.getChessMan(xNext, yNext) == null
                    || cb.getChessMan(x + xChange, y + yChange).toBool() == my_turn) {
                return AllDef.ERROR;
            }
            if (cb.getChessMan(xNext, yNext).toBool() == my_turn) {
                while (x != xNext || y != yNext) {
                    if (currX != x || currY != y) {
                        cb.setOldChessMan(x, y, my_turn);
                    }
                    x = x + xChange;
                    y = y + yChange;
                    cb.changeBoard(x, y, my_turn);

                }
                break;
            }
            xNext = xNext + xChange;
            yNext = yNext + yChange;
        }
        return AllDef.OK;
    }

    private boolean judge(int x, int y, int xChange, int yChange) {
        int xNext = x + xChange;
        int yNext = y + yChange;
        if (cb.getChessMan(x, y) != null) {
            return AllDef.ERROR;
        }
        while (true) {
            if (xNext < 1 || xNext > 8 || yNext < 1 || yNext > 8
                    || cb.getChessMan(xNext, yNext) == null
                    || cb.getChessMan(x + xChange, y + yChange).toBool() == my_turn) {
                return AllDef.ERROR;
            }
            if (cb.getChessMan(xNext, yNext).toBool() == my_turn) {
                break;
            }
            xNext = xNext + xChange;
            yNext = yNext + yChange;
        }
        return AllDef.OK;
    }

    //change the chess board array
    public boolean setChess(int x, int y) {
        boolean result = false;
        result = eat(x, y, 0, -1) || result;
        result = eat(x, y, 0, 1) || result;
        result = eat(x, y, -1, -1) || result;
        result = eat(x, y, -1, 0) || result;
        result = eat(x, y, -1, 1) || result;
        result = eat(x, y, 1, -1) || result;
        result = eat(x, y, 1, 0) || result;
        result = eat(x, y, 1, 1) || result;

        if (AllDef.OK == result) {
            cb.changeBoard(x, y, my_turn);

        }
        return result;
    }

    public ChessMan getChess(int x, int y) {
        return cb.getChessMan(x, y);
    }

    public void init() {
        cb.initChessBoard();
    }

    //check out who is the winner. 
    public int result() {
        int x, y;
        boolean result = false;

        for (x = 1; x <= 8 && false == result; x++) {
            for (y = 1; y <= 8 && false == result; y++) {
                result = judge(x, y, 0, -1) || result;
                result = judge(x, y, 0, 1) || result;
                result = judge(x, y, -1, -1) || result;
                result = judge(x, y, -1, 0) || result;
                result = judge(x, y, -1, 1) || result;
                result = judge(x, y, 1, -1) || result;
                result = judge(x, y, 1, 0) || result;
                result = judge(x, y, 1, 1) || result;
            }
        }
        if (false == result) {
            return AllDef.NOSET;
        }

        for (x = 1; x <= 8; x++) {
            for (y = 1; y <= 8; y++) {
                if (cb.getChessMan(x, y) != null && cb.getChessMan(x, y).toBool() != my_turn) {
                    return AllDef.NOWIN;
                }
            }
        }
        return AllDef.WIN;
    }

    //get the number of black and white pieces on the board
    public int getNum() {
        int num = 0;
        for (int x = 1; x <= 8; x++) {
            for (int y = 1; y <= 8; y++) {
                if (cb.getChessMan(x, y) != null && cb.getChessMan(x, y).toBool() == my_turn) {
                    num++;
                }
            }
        }
        return num;
    }

    public ChessMan getOldChess(int x, int y) {
        return cb.getOldChessMan(x, y);
    }

    public void initOldChess() {
        cb.initOldChessBoard();
    }
}
